package com.chen.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.chen.pojo.User;
import com.chen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @date 2022/3/11 11:39
 */
@Component
public class TokenUtils {

    /**
     * 生成token
     * @return
     */

    private static UserService staticUser1Service;

    @Autowired
    private UserService users1Service;
    @PostConstruct
    public void setUsers1Service(){
        staticUser1Service=users1Service;
    }



    private static final long EXPIRE_TIME = 15 *1000;
    public static String genToken(String userId,String sign){

        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        return   JWT.create().withAudience(userId) // 将 user id 作为载荷
//                .withExpiresAt(DateUtil.offsetHour(new Date(),2)) //2小时后token过期
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(sign));
    }


}

